﻿using System;

namespace PrimeNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入上限和下限，用空格分隔：");
            string input = Console.ReadLine();
            string[] inputs = input.Split(' ');

            if (inputs.Length != 2)
            {
                Console.WriteLine("输入格式错误，请输入两个数字并用空格分隔！");
                return;
            }

            if (!int.TryParse(inputs[0], out int lower) || !int.TryParse(inputs[1], out int upper))
            {
                Console.WriteLine("输入无效，请输入有效的整数！");
                return;
            }

            if (lower > upper)
            {
                Console.WriteLine("上限不能小于下限！");
                return;
            }

            Console.WriteLine($"在 {lower} 到 {upper} 之间的素数如下：");
            int count = 0;

            for (int i = lower; i <= upper; i++)
            {
                if (IsPrime(i))
                {
                    Console.Write(i + " ");
                    count++;
                    if (count % 10 == 0)
                    {
                        Console.WriteLine();
                    }
                }
            }

            if (count == 0)
            {
                Console.WriteLine("\n在指定范围内没有找到素数。");
            }
            else
            {
                Console.WriteLine();
            }
        }

        static bool IsPrime(int number)
        {
            if (number <= 1) return false;
            if (number == 2) return true;
            if (number % 2 == 0) return false;

            for (int i = 3; i * i <= number; i += 2)
            {
                if (number % i == 0) return false;
            }

            return true;
        }
    }
}